import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_demo/model/play_model.dart';
import 'package:flutter_demo/view/recorded_list_view.dart';
import 'package:path_provider/path_provider.dart';

class DiscoveryPage extends StatefulWidget {
  @override
  _DiscoveryPageState createState() => _DiscoveryPageState();
}

class _DiscoveryPageState extends State<DiscoveryPage> {

  Directory appDirectory;
  Stream<FileSystemEntity> fileStream;
  List<String> records;
  List<PlayModel> players;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getData();
  }

  getData(){
    records = [];
    players = [];
    getApplicationDocumentsDirectory().then((value) {
      appDirectory = value;
      appDirectory.list().listen((onData) {
        String path = onData.path;
        print(path);
        records.add(path);
        PlayModel model = PlayModel();
        model.playState = PlayState.isStop;
        model.completedPercentage = 0.0;
        model.isSelected = false;
        model.totalDuration = 0;
        model.currentDuration = 0;
        model.path = path;
        players.add(model);
      }).onDone(() {
        records = records.reversed.toList();
        setState(() {});
      });
    });
  }
  @override
  void dispose() {
    appDirectory = null;
    records = null;
    players = [];
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('发现'),
      ),
      body: Container(
        child: RecordListView(records: records,players: players,)
      ),
    );
  }
}
